function comeBackToHistory() {
	$('#currentBidRate').text($('#initialBidFormatted').val());
	$('#currentAskRate').text($('#initialAskFormatted').val());
	$('#currentEvolution').text($('#initialEvolution').val());
	$('#currentEvolution').attr('class', $('#initialEvolutionClass').val());
	$('#arrowEvolution').css('display', 'block');
	$('#tradeSellBidRateHidden').val($('#initialBid').val());
	$('#tradeSellBidUnits').trigger('propertychange');
	$('#tradeSellAskRateHidden').val($('#initialAsk').val());
	$('#tradeSellAskUnits').trigger('propertychange');
	$('#tradeBuyAskRateHidden').val($('#initialAsk').val());
	$('#tradeBuyAskUnits').trigger('propertychange');
	$('#tradeBuyBidRateHidden').val($('#initialBid').val());
	$('#tradeBuyBidUnits').trigger('propertychange');
}

function graphHistory(nbDays, divGraph, oldRateAsk, oldRateDate) {
	Highcharts.setOptions({
		global: {
			useUTC: false
		}
	});
	var chart;
	chart = new Highcharts.Chart({
		chart: {
			renderTo: divGraph,
			marginRight: 10
		},
		title: {
			text: nbDays + ' days rate chart'
		},
		xAxis: {
			type: 'datetime',
			tickPixelInterval: 150
		},
		yAxis: {
			title: {
				text: 'Rate'
			},
			plotLines: [{
				value: 0,
				width: 1,
				color: '#808080'
			}]
		},
		tooltip: {
			formatter: function() {
				return Highcharts.dateFormat('%Y/%m/%d', this.x) + '<br/>' +
					Highcharts.numberFormat(this.y, 4);
			}
		},
		legend: {
			enabled: false
		},
		exporting: {
			enabled: false
		},
		series: [{
			name: 'Data',
			data: (function() {
				var data = [];
				for (var i = 30 - nbDays; i <= 29; i++) {
					data.push({
						x: oldRateDate[i],
						y: parseFloat(oldRateAsk[i])
					});
				}
				return data;
			})()
		}]
	});
}

function graphLive(oldRateAsk, oldRateDate) {
	var initialAskRate = parseFloat(oldRateAsk[29]);
	var currentGraphDate = new Date(oldRateDate[29].getTime() - 29 * 5000);
	var currentAskRate = initialAskRate;
	var currentBidRate;
	var currentUnits;
	$('#initialEvolution').val($('#currentEvolution').text());
	$('#initialEvolutionClass').val($('#currentEvolution').attr('class'));
	Highcharts.setOptions({
		global: {
			useUTC: false
		}
	});
	var chart;
	chart = new Highcharts.Chart({
		chart: {
			renderTo: 'graph-container-live',
			marginRight: 10,
			events: {
				load: function() {
					// set up the updating of the chart each second
					var series = this.series[0];
					setInterval(function() {
						if ($('#graph-container-live').css('display') == 'block') {
							currentGraphDate = new Date(currentGraphDate.getTime() + 5000);
							currentAskRate = currentAskRate * (0.9999 + (Math.round(10000 * (Math.random() / 5000)) / 10000));
							if (initialAskRate - currentAskRate > 0.0005) {
								currentAskRate = initialAskRate - 0.0004;
							}
							if (currentAskRate - initialAskRate > 0.0005) {
								currentAskRate = initialAskRate + 0.0004;
							}
							var y = Math.random();
							series.addPoint([currentGraphDate.getTime(), currentAskRate], true, true);
							currentBidRate = 0.9997 * currentAskRate;
							$('#currentBidRate').text(Math.round(currentBidRate * 10000) / 10000);
							$('#currentAskRate').text(Math.round(currentAskRate * 10000) / 10000);
							$('#currentEvolution').text('-');
							$('#currentEvolution').removeClass('rate-decrease rate-increase rate-same');
							$('#arrowEvolution').css('display', 'none');
							currentUnits = parseFloat($('#tradeSellBidUnits').val());
							if (isNaN(currentUnits)) {
								currentUnits = 0;
							}
							$('#tradeSellBidRateHidden').val(currentBidRate);
							$('#tradeSellBidCost').text(Math.round($('#tradeSellBidUnits').val() * currentBidRate * 100) / 100);
							currentUnits = parseFloat($('#tradeSellAskUnits').val());
							if (isNaN(currentUnits)) {
								currentUnits = 0;
							}
							$('#tradeSellAskRateHidden').val(currentAskRate);
							$('#tradeSellAskCost').text(Math.round($('#tradeSellAskUnits').val() / currentAskRate * 100) / 100);
							currentUnits = parseFloat($('#tradeBuyAskUnits').val());
							if (isNaN(currentUnits)) {
								currentUnits = 0;
							}
							$('#tradeBuyAskRateHidden').val(currentAskRate);
							$('#tradeBuyAskCost').text(Math.round($('#tradeBuyAskUnits').val() * currentAskRate * 100) / 100);
							currentUnits = parseFloat($('#tradeBuyBidUnits').val());
							if (isNaN(currentUnits)) {
								currentUnits = 0;
							}
							$('#tradeBuyBidRateHidden').val(currentBidRate);
							$('#tradeBuyBidCost').text(Math.round($('#tradeBuyBidUnits').val() / currentBidRate * 100) / 100);
						}
					}, 5000);
				}
			}
		},
		title: {
			text: 'Live rate chart'
		},
		xAxis: {
			type: 'datetime',
			tickPixelInterval: 150
		},
		yAxis: {
			title: {
				text: 'Rate'
			},
			plotLines: [{
				value: 0,
				width: 1,
				color: '#808080'
			}]
		},
		tooltip: {
			formatter: function() {
				return Highcharts.dateFormat('%Y/%m/%d %H:%M:%S', this.x) + '<br/>' +
					Highcharts.numberFormat(this.y, 4);
			}
		},
		legend: {
			enabled: false
		},
		exporting: {
			enabled: false
		},
		series: [{
			name: 'Data',
			data: (function() {
				// generate an array of random data
				var data = [];
				for (var i = -29; i <= 0; i++) {
					currentGraphDate = new Date(currentGraphDate.getTime() + 5000);
					currentAskRate = currentAskRate * (0.9999 + ((Math.round(10000 * Math.random()) / 10000) / 5000));
					if (initialAskRate - currentAskRate > 0.0005) {
						currentAskRate = initialAskRate - 0.0004;
					}
					if (currentAskRate - initialAskRate > 0.0005) {
						currentAskRate = initialAskRate + 0.0004;
					}
					data.push({
						x: currentGraphDate,
						y: currentAskRate
					});
				}
				return data;
			})()
		}]
	});
}

$(document).ready(function() {
	var oldRateAskLoaded = $(".oldRateAsk");
	var oldRateAsk = new Array();
	var oldRateDateLoaded = $(".oldRateDate");
	var oldRateDate = new Array();
	var nbLoadedValues = oldRateAskLoaded.size();
	var nbMissing = 30 - nbLoadedValues;
	var currentDateLoaded = oldRateDateLoaded[0].value;
	var currentDate = new Date();
	currentDate.setFullYear(
		currentDateLoaded.substring(0, 4),
		currentDateLoaded.substring(5, 7) - 1,
		currentDateLoaded.substring(8, 10)
	);
	var currentDateTimestamp = currentDate.getTime();
	for (var i = 0; i < nbMissing; ++i) {
		oldRateAsk[i] = oldRateAskLoaded[nbLoadedValues - 1].value;
	}
	for (var i = nbMissing; i < 30; ++i) {
		oldRateAsk[i] = oldRateAskLoaded[29 - i].value;
	}
	var nbDaysWeekend = 0;
	for (var i = 0; i < 30; ++i) {
		oldRateDate[29 - i] = new Date(currentDateTimestamp - (i + nbDaysWeekend) * 86400000);
		if (oldRateDate[29 - i].getDay() == 0) {
			nbDaysWeekend += 2;
			oldRateDate[29 - i] = new Date(currentDateTimestamp - (i + nbDaysWeekend) * 86400000);
		} else if (oldRateDate[29 - i].getDay() == 6) {
			nbDaysWeekend += 1;
			oldRateDate[29 - i] = new Date(currentDateTimestamp - (i + nbDaysWeekend) * 86400000);
		}
	}
	graphHistory(10, 'graph-container-10', oldRateAsk, oldRateDate);
	graphHistory(30, 'graph-container-30', oldRateAsk, oldRateDate);
	$('#graph-container-30').css('display', 'none');
	graphLive(oldRateAsk, oldRateDate);
	$('#graph-container-live').css('display', 'none');

	$('#graph10').click(function() {
		$('#graph-container-10').css('display', 'block');
		$('#graph-container-30').css('display', 'none');
		$('#graph-container-live').css('display', 'none');
		comeBackToHistory();
		return false;
	});
	$('#graph30').click(function() {
		$('#graph-container-10').css('display', 'none');
		$('#graph-container-30').css('display', 'block');
		$('#graph-container-live').css('display', 'none');
		comeBackToHistory();
		return false;
	});
	$('#graphLive').click(function() {
		$('#graph-container-10').css('display', 'none');
		$('#graph-container-30').css('display', 'none');
		$('#graph-container-live').css('display', 'block');
		return false;
	});

	$('#tradeSellBidUnits').bind("propertychange keyup input paste", function(event) {
		var units = parseFloat($(this).val());
		if (isNaN(units)) {
			units = 0;
		}
		var currentBidRate = parseFloat($('#currentBidRate').text());
		newSellBidCost = Math.round(units * currentBidRate * 100) / 100;
		$('#tradeSellBidCost').text(newSellBidCost);
	});
	$('#tradeSellBid').submit(function() {
		var units = parseFloat($("#tradeSellBidUnits").val());
		if (isNaN(units)) {
			units = 0;
		}
		$("#tradeSellBidUnits").val(units);
		return ($('#tradeSellBidCost').text() != '0');
	});

	$('#tradeSellAskUnits').bind("propertychange keyup input paste", function(event) {
		var units = parseFloat($(this).val());
		if (isNaN(units)) {
			units = 0;
		}
		var currentAskRate = parseFloat($('#currentAskRate').text());
		newSellAskCost = Math.round(units / currentAskRate * 100) / 100;
		$('#tradeSellAskCost').text(newSellAskCost);
	});
	$('#tradeSellAsk').submit(function() {
		var units = parseFloat($("#tradeSellAskUnits").val());
		if (isNaN(units)) {
			units = 0;
		}
		$("#tradeSellAskUnits").val(units);
		return ($('#tradeSellAskCost').text() != '0');
	});

	$('#tradeBuyAskUnits').bind("propertychange keyup input paste", function(event) {
		var units = parseFloat($(this).val());
		if (isNaN(units)) {
			units = 0;
		}
		var currentAskRate = parseFloat($('#currentAskRate').text());
		newBuyAskCost = Math.round(units * currentAskRate * 100) / 100;
		$('#tradeBuyAskCost').text(newBuyAskCost);
	});
	$('#tradeBuyAsk').submit(function() {
		var units = parseFloat($("#tradeBuyAskUnits").val());
		if (isNaN(units)) {
			units = 0;
		}
		$("#tradeBuyAskUnits").val(units);
		return ($('#tradeBuyAskCost').text() != '0');
	});

	$('#tradeBuyBidUnits').bind("propertychange keyup input paste", function(event) {
		var units = parseFloat($(this).val());
		if (isNaN(units)) {
			units = 0;
		}
		var currentBidRate = parseFloat($('#currentBidRate').text());
		newBuyBidCost = Math.round(units / currentBidRate * 100) / 100;
		$('#tradeBuyBidCost').text(newBuyBidCost);
	});
	$('#tradeBuyBid').submit(function() {
		var units = parseFloat($("#tradeBuyBidUnits").val());
		if (isNaN(units)) {
			units = 0;
		}
		$("#tradeBuyBidUnits").val(units);
		return ($('#tradeBuyBidCost').text() != '0');
	});
});
